﻿namespace dk.Helper.Base.Core
{
    public class CurrentVillageInfo : ICurrentVillageInfo
    {
        private readonly IWorld world;
        private IVillage village;

        /// <summary>
        /// Initializes a new instance of the <see cref="CurrentVillageInfo"/> class.
        /// </summary>
        /// <param name="world">The world.</param>
        public CurrentVillageInfo(IWorld world)
        {
            this.world = world;
        }

        #region ICurrentVillageInfo Members

        /// <summary>
        /// Gets or sets the village.
        /// </summary>
        /// <value>
        /// The village.
        /// </value>
        public IVillage Village
        {
            get { return village; }
            set
            {
                village = value;
                Player = GetPlayer(Village);
                Ally = GetAlly(Player);
            }
        }

        /// <summary>
        /// Gets the player.
        /// </summary>
        public IPlayer Player { get; private set; }

        /// <summary>
        /// Gets the ally.
        /// </summary>
        public IAlly Ally { get; private set; }

        #endregion

        /// <summary>
        /// Gets the player.
        /// </summary>
        /// <param name="village_in">The village_in.</param>
        /// <returns></returns>
        private IPlayer GetPlayer(IVillage village_in)
        {
            if (village_in == null || village_in.PlayerID == 0)
                return null;

            return world.Players.PlayersByID[village_in.PlayerID];
        }

        /// <summary>
        /// Gets the ally.
        /// </summary>
        /// <param name="player_in">The player_in.</param>
        /// <returns></returns>
        private IAlly GetAlly(IPlayer player_in)
        {
            if (player_in == null || player_in.AllyID == 0)
                return null;

            return world.Allies.AlliesByID[player_in.AllyID];
        }
    }
}